﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using _02_WebService.ServiceReference1; // 引用WebService的命名空间

namespace _02_WebService
{
    public partial class Form1 : Form
    {
        BindingList<Province> ProvinceList = new BindingList<Province>();
        BindingList<City> CityList = new BindingList<City>();
        BindingList<string> infoList = new BindingList<string>();

        public Form1()
        {
            InitializeComponent();
            //绑定下拉列表的数据库，只要显示省份名称即可
            comboBox1.DataSource = ProvinceList;
            //绑定下拉列表的数据库，只要显示城市名称即可
            comboBox2.DataSource = CityList;
            //绑定天气信息的数据库
            listBox1.DataSource = infoList;

        }

        /// <summary>
        /// 获取省份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button1_Click(object sender, EventArgs e)
        {
            //提示用户正在获取省份
            toolStripStatusLabel2.Text = "正在获取省份...";
            comboBox1.SelectedValueChanged -= new EventHandler(comboBox1_SelectedValueChanged);

            WeatherWSSoapClient weatherWSSoapClient = new WeatherWSSoapClient("WeatherWSSoap");
            //调用WebService的方法,获取所有省份
            DataSet provinces = weatherWSSoapClient.getRegionDataset();
            //获取省份数据，将数据添加到CityList中
            foreach (DataRow row in provinces.Tables[0].Rows)
            {
                Province city = new Province();
                city.RegionID = row["RegionID"].ToString();
                city.RegionName = row["RegionName"].ToString();
                ProvinceList.Add(city);
            }
            //设置下拉列表的显示项第一个
            comboBox1.SelectedIndex = 0;
            //添加完毕后绑定事件
            comboBox1.SelectedValueChanged += new EventHandler(comboBox1_SelectedValueChanged);
            //提示用户获取省份完毕
            toolStripStatusLabel2.Text = "获取省份完毕";

        }
        /// <summary>
        /// 选择的省份发生变化时，获取城市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {

            //提示用户正在获取城市
            toolStripStatusLabel2.Text = "正在获取城市...";
            //绑定事件
            this.comboBox2.SelectedValueChanged -= new System.EventHandler(this.comboBox2_SelectedValueChanged);


            //获取选中的省份
            Province selectedCity = comboBox1.SelectedItem as Province;
            if (selectedCity != null)
            {
                //获取选中的省份ID
                string regionID = selectedCity.RegionID;
                WeatherWSSoapClient weatherWSSoapClient = new WeatherWSSoapClient("WeatherWSSoap");
                //调用WebService的方法,获取所有城市
                DataSet cities = weatherWSSoapClient.getSupportCityDataset(regionID);
                //获取城市数据，将数据添加到CityList中
                CityList.Clear();
                foreach (DataRow row in cities.Tables[0].Rows)
                {
                    City city = new City();
                    city.CityID = row["CityID"].ToString();
                    city.CityName = row["CityName"].ToString();
                    CityList.Add(city);
                }
                //提示用户获取城市完毕
                toolStripStatusLabel2.Text = "获取城市完毕";
                //绑定事件
                this.comboBox2.SelectedValueChanged += new System.EventHandler(this.comboBox2_SelectedValueChanged);

            }
            else
            {
                //如果没有选中省份，则清空城市列表
                CityList.Clear();
                //提示用户没有选中省份
                toolStripStatusLabel2.Text = "没有选中省份";
            }
        }
        /// <summary>
        /// 选定的城市发生变化时，获取天气
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            //提示用户正在获取天气
            toolStripStatusLabel2.Text = "正在获取天气...";

            //获取选中的城市
            City selectedCity = comboBox2.SelectedItem as City;
            if (selectedCity != null)
            {
                try
                {
                    //获取选中的城市ID
                    string cityID = selectedCity.CityID;
                    WeatherWSSoapClient weatherWSSoapClient = new WeatherWSSoapClient("WeatherWSSoap");
                    //调用WebService的方法,获取天气信息
                    string[] weatherInfo = weatherWSSoapClient.getWeather(cityID,"");
                    //获取天气数据，将数据添加到infoList中
                    infoList.Clear();
                    foreach (string info in weatherInfo)
                    {
                        infoList.Add(info);
                    }
                    //提示用户获取天气完毕
                    toolStripStatusLabel2.Text = "获取天气完毕";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

            }
            else
            {
                //如果没有选中城市，则清空天气信息列表
                infoList.Clear();
                //提示用户没有选中城市
                toolStripStatusLabel2.Text = "没有选中城市";
            }

        }
    }
}
